﻿using _8Group.MES.ISongService.MdWorkshop;
using _8Group.MES.ISongService.WmItemRecpt;
using _8Group.MES.ISongService.WmItemRecpt.DTO;
using _8Group.MES.Model.WM仓储管理;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Yitter.IdGenerator;

namespace _8Group.MES.SongService
{
    [ApiExplorerSettings(GroupName = "宋恒漳")]
    public class WmItemRecptService : ApplicationService, IWmItemRecptService
    {
        private readonly IRepository<WM_Item_Recpt, int> _wmItemRecptRepository;

        public WmItemRecptService(IRepository<WM_Item_Recpt, int> wmItemRecptRepository)
        {
            _wmItemRecptRepository = wmItemRecptRepository;
        }


        /// <summary>
        /// 获取采购入库列表
        /// </summary>
        /// <param name="itemRecptDTO"></param>
        /// <returns></returns>
        public async Task<object> GetWmItemRecptListAsync(RequestWmItemRecptDTO itemRecptDTO)
        {
            var wmItemRecptList = await _wmItemRecptRepository.GetQueryableAsync();
            wmItemRecptList = wmItemRecptList.WhereIf(!string.IsNullOrEmpty(itemRecptDTO.RecptCode), x => x.RecptCode.Contains(itemRecptDTO.RecptCode));
            wmItemRecptList = wmItemRecptList.WhereIf(!string.IsNullOrEmpty(itemRecptDTO.RecptName), x => x.RecptName.Contains(itemRecptDTO.RecptName));
            wmItemRecptList = wmItemRecptList.WhereIf(!string.IsNullOrEmpty(itemRecptDTO.PoCode), x => x.PoCode.Contains(itemRecptDTO.PoCode));
            wmItemRecptList = wmItemRecptList.WhereIf(!string.IsNullOrEmpty(itemRecptDTO.VendorName), x => x.VendorName.Contains(itemRecptDTO.VendorName));
            wmItemRecptList = wmItemRecptList.WhereIf(!string.IsNullOrEmpty(itemRecptDTO.RecptDate), x => x.RecptDate.ToString().Contains(itemRecptDTO.RecptDate));
            var count = wmItemRecptList.Count();
            wmItemRecptList = wmItemRecptList.OrderBy(x => x.Id).Skip(((itemRecptDTO.Page - 1) ?? 0) * (itemRecptDTO.Limit ?? 10)).Take(itemRecptDTO.Limit ?? 10);
            var result = new ResponseModel
            {
                code = "",
                count = count,
                data = wmItemRecptList.ToList(),
                msg = "",
            };
            return result;
        }

        /// <summary>
        /// 删除采购入库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteWmItemRecptAsync(int id)
        {
            var wmItemRecpt = await _wmItemRecptRepository.GetAsync(id);
            await _wmItemRecptRepository.DeleteAsync(wmItemRecpt, true);
            return true;
        }

        /// <summary>
        /// 新增采购入库
        /// </summary>
        /// <param name="createWmItemRecptDTO"></param> 
        /// <returns></returns>
        public async Task<bool> AddWmItemRecptAsync(AddWmItemRecptDTO addWmItemRecptDTO)
        {
            //判断名称是否重复  
            var wmItemRecpt = await _wmItemRecptRepository.FirstOrDefaultAsync(x => x.RecptName == addWmItemRecptDTO.RecptName);
            if (wmItemRecpt!= null)
            {
               return false;
            }
            addWmItemRecptDTO.CreateBy = "宋恒漳";
            addWmItemRecptDTO.CreateTime = DateTime.Now;
            addWmItemRecptDTO.PoCode=YitIdHelper.NextId().ToString();
            wmItemRecpt = AutoMapperExt.MapTo<WM_Item_Recpt>(addWmItemRecptDTO);
            await _wmItemRecptRepository.InsertAsync(wmItemRecpt);  
            return true;
        }

        /// <summary>
        /// 修改采购入库
        /// </summary>
        /// <param name="updateWmItemRecptDTO"></param>
        /// <returns></returns>
        public async Task<bool> UpdateWmItemRecptAsync(UpdateWmItemRecptDTO updateWmItemRecptDTO)
        {
            updateWmItemRecptDTO.UpdateBy = "宋恒漳";
            updateWmItemRecptDTO.UpdateTime = DateTime.Now;
            var wmItemRecpt = AutoMapperExt.MapTo<WM_Item_Recpt>(updateWmItemRecptDTO);
            await _wmItemRecptRepository.UpdateAsync(wmItemRecpt);
            return true;
        }

        /// <summary>
        /// 更新采购入库状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
       public async Task<bool> UpdateWmItemRecptStatusAsync(int id)
        {
            //查询ID对应的采购入库
            var wmItemRecpt = await _wmItemRecptRepository.GetAsync(id);
            if (wmItemRecpt == null)
            {
                return false;
            }
            //判断采购入库状态
            if (wmItemRecpt.Status == "PREPARE")
            {
                wmItemRecpt.Status = "Completed";
            }
            else
            {
                wmItemRecpt.Status = "PREPARE";
            }
            await _wmItemRecptRepository.UpdateAsync(wmItemRecpt);
            return true;
        }
    }
}
